package com.yx.cloud.view;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;
import com.yx.cloud.R;
import com.yx.cloud.adapter.MusicTitleTabAdapter;
import com.yx.cloud.bean.MusicTabBean;
import com.yx.cloud.fragment.MusicListFragment;
import com.yx.cloud.product.sdk.tmall.MusicInfo;
import com.yx.cloud.product.sdk.tmall.AliGenieSdk;
import com.iot.common.utils.Event;
import java.util.ArrayList;
import java.util.List;

public class MusicBottomSheetDialog extends DialogFragment implements MusicTitleTabAdapter.InnerItemOnclickListener{

    private MusicTitleTabAdapter musicTitleTabAdapter;
    private List<MusicTabBean> musicType;
    private List<MusicListFragment> fragmentList;
    private FragmentManager fm;
    private MusicViewPager musicViewPager;
    private RecyclerView recyclerView;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置style
        setStyle(DialogFragment.STYLE_NORMAL, R.style.BottomDialog);
    }

    @Override
    public void onStart() {
        super.onStart();
        //设置 dialog 的宽高
        getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置 dialog 的背景为 null
        getDialog().getWindow().setBackgroundDrawable(null);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //去除标题栏
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.BOTTOM; //底部
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(lp);
        View view = inflater.inflate(R.layout.music_list_view, container, false);
        return view;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
        initView(view);
        initViewPager();
    }

    private void initView(View view){
        //设置线性布局管理器类型
        recyclerView = view.findViewById(R.id.rv_music_title_tab_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), RecyclerView.HORIZONTAL, false));
        musicTitleTabAdapter = new MusicTitleTabAdapter(getContext(), musicType);
        musicTitleTabAdapter.setOnInnerItemOnClickListener(this);
        recyclerView.setAdapter(musicTitleTabAdapter);
        musicViewPager = view.findViewById(R.id.vp_music);
    }

    private void initData(){
        //TODO 获取音乐列表tab类型
        musicType = new ArrayList<>();
        MusicTabBean musicTabBean0 = new MusicTabBean();
        musicTabBean0.setCateId(MusicListFragment.CATE_TYPE_PLAYING_LIST);
        musicTabBean0.setMusicTopicName(getResources().getString(R.string.M_Playlist));
        musicType.add(musicTabBean0);
        MusicTabBean musicTabBean1 = new MusicTabBean();
        musicTabBean1.setCateId(MusicListFragment.CATE_TYPE_PLAY_LIST_HISTORY);
        musicTabBean1.setMusicTopicName(getResources().getString(R.string.M_PlayHistory));
        musicType.add(musicTabBean1);
    }

    private void getMusicListCateInfo(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                AliGenieSdk.get().getMusicListCateInfo();
            }
        }).start();
    }

    private void initViewPager() {
        fragmentList = new ArrayList<>();
        for(int i = 0;i < musicType.size();i++){
            MusicListFragment musicListFragment = new MusicListFragment(musicType.get(i).getCateId(),"ALBUM");
            fragmentList.add(musicListFragment);
        }

        fm = getChildFragmentManager();
        FragAdapter adapter = new FragAdapter(fm, fragmentList);
        musicViewPager.setAdapter(adapter);
        musicViewPager.setOffscreenPageLimit(10);
        musicViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                selectedTab(position);

                //横向recycleView中item居中显示
                int firstPosition = recyclerView.getChildLayoutPosition(recyclerView.getChildAt(0));
                int lastPosition = recyclerView.getChildLayoutPosition(recyclerView.getChildAt(recyclerView.getChildCount() - 1));
                View view1 = recyclerView.getChildAt(position - firstPosition);
                View view2 = recyclerView.getChildAt(lastPosition - position);
                if(view1 == null || view2 == null){
                    return;
                }else {
                    int left = recyclerView.getChildAt(position - firstPosition).getLeft();
                    int right = recyclerView.getChildAt(lastPosition - position).getLeft();
                    recyclerView.scrollBy((left - right) / 2,0);
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void selectedTab(int position){
        musicTitleTabAdapter.selectItem(position);
    }

    @Override
    public void itemClick(View v, int position) {
        //TODO 选中tab高亮显示，并且切换相应音乐列表
        musicTitleTabAdapter.notifyDataSetChanged();
        musicViewPager.setCurrentItem(position);
    }

    public class FragAdapter extends FragmentPagerAdapter {

        private List<MusicListFragment> mFragments;

        public FragAdapter(FragmentManager fm, List<MusicListFragment> fragments) {
            super(fm);
            mFragments = fragments;
        }

        @Override
        public MusicListFragment getItem(int arg0) {
            return mFragments.get(arg0);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

    }

    public interface MusicListOperation {
        void createPlayList(int index);

        void play();

        void pause();

        void next();

        void previous();

        void updateData();

        List<MusicInfo> getMusicInfoList();

        void onDataUpdate(Event event);
    }

}
